import {Injectable} from "@angular/core";

@Injectable({
  providedIn: 'root'
})
export class LoginAdmin {
  username: string;
  name: string;
  authorities: any[];
  isLogin: boolean;
  jwt: string;
  jwtExpire: string;

  constructor() {
    this.load();
  }

  setValue(loginAdmin: LoginAdmin) {
    this.username = loginAdmin.username;
    this.name = loginAdmin.name;
    this.authorities = loginAdmin.authorities;
    this.isLogin = loginAdmin.isLogin;
    if (loginAdmin.jwt) {
      this.jwt = loginAdmin.jwt;
      this.jwtExpire = loginAdmin.jwtExpire;
    }
  }

  clean() {
    this.username = null;
    this.name = null;
    this.authorities = null;
    this.isLogin = false;
    this.jwt = null;
    this.jwtExpire = null;
    this.save();
  }

  save() {
    sessionStorage.setItem('loginAdmin', JSON.stringify(this));
  }

  load() {
    const json = sessionStorage.getItem('loginAdmin');
    if (json) {
      const storageUser = JSON.parse(json);
      this.setValue(storageUser);
    }
  }


}
